<template>
  <div>
    <!-- 添加按钮 -->
    <div class="btn">
      <el-button size="small" type="success" @click="addShow">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="brandList" style="width: 100%">
      <el-table-column prop="name" label="品牌名称" width="500px" />
      <el-table-column prop="logo" label="品牌图标" width="500px">
        <template #default="scope">
          <img :src="scope.row.logo" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="500px" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[1, 2, 3, 10]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="loadData"
      @current-change="loadData"
      style="margin-top: 20px"
    />
    <!-- 弹框 -->
    <el-dialog v-model="dialogFormVisible" title="添加或修改">
      <el-form :model="brand">
        <el-form-item label="品牌名称" label-width="120px">
          <el-input v-model="brand.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="品牌图标" label-width="120px">
          <el-upload
            class="avatar-uploader"
            name="file"
            :headers="headers"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :on-error="uploadError"
          >
            <img v-if="brand.logo" :src="brand.logo" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = dialogShowRole = false">
            取消
          </el-button>
          <el-button type="primary" @click="updateOrAdd">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useApp } from '@/pinia/modules/app'
import {
  GetBrandList,
  GetBrandById,
  DeleteBrand,
  UpdateBrand,
  SaveBrand,
} from '@/api/brand'
import { ElMessage, ElMessageBox } from 'element-plus'
//表格数据
const brandList = ref([])
//dialog
const dialogFormVisible = ref(false)
const brand = ref({})
//页面挂载
const pageNum = ref(1)
const pageSize = ref(3)
const total = ref(0)
const loadData = async () => {
  const { data } = await GetBrandList(pageNum.value, pageSize.value)
  brandList.value = data.list
  total.value = data.total
}

onMounted(() => {
  loadData()
})
//添加
const addShow = () => {
  dialogFormVisible.value = true
  brand.value = {}
}
const handlerSave = async () => {
  await SaveBrand(brand.value)
  loadData()
  dialogFormVisible.value = false
  ElMessage.success('添加品牌成功')
}

//修改
const handleEdit = async row => {
  dialogFormVisible.value = true
  const { data } = await GetBrandById(row.id)
  brand.value = data
}

const handlerUpdate = async () => {
  await UpdateBrand(brand.value)
  loadData()
  dialogFormVisible.value = false
  ElMessage.success('修改品牌成功')
}

//添加||修改
const updateOrAdd = () => {
  if (brand.value.id) {
    //修改
    handlerUpdate()
  } else {
    //添加
    handlerSave()
  }
}

//删除
const handleDelete = row => {
  ElMessageBox.confirm(`确认删除【${row.name}】品牌吗?`, 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      await DeleteBrand(row.id)
      loadData()
    })
    .catch(() => {
      ElMessage({
        type: 'success',
        message: '取消删除',
      })
    })
}

//文件上传
//设置token
const headers = ref({ token: useApp().authorization.token })
//上传路径(不能写localhost)
const uploadUrl = ref('http://127.0.0.1:8081/admin/system/file/upload/logo')
//上传成功
const handleAvatarSuccess = respose => {
  if (+respose.code === 200) {
    brandList.value.logo = respose.data
    brand.value.logo = respose.data
    ElMessage.success('上传成功')
  } else {
    ElMessage.error(respose.message)
  }
}
//上传失败
const uploadError = error => {
  ElMessage.error('网络错误')
}
</script>
<style>
.btn {
  background-color: #fff;
  height: 50px;
  line-height: 50px;
  margin-bottom: 15px;
  padding-left: 15px;
}

/* 上传文件 */
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
